package com.dsl.tms.util;


import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class JsonUtil {

    private static String json;

    /**
     * 将对象转化为json
     * @param t
     * @param <T>
     * @return
     * @throws JsonProcessingException
     */
    public static <T> String toJson(T t) throws JsonProcessingException {
        return OBJECT_MAPPER.get().writeValueAsString(t);
    }

    /**
     * 将json转化为实体类
     * @param json
     * @param valueType
     * @param <T>
     * @return
     * @throws IOException
     */
    public static <T> T toEntity(String json,Class<T> valueType) throws IOException{
        return OBJECT_MAPPER.get().readValue(json,valueType);
    }

    /**
     * 将json类转化为List
     * @param json
     * @param collectionClass
     * @param elementClass
     * @param <T>
     * @return
     * @throws IOException
     */
    public static <T> List<T> toList(String json, Class<? extends List> collectionClass,
                                     Class<T> elementClass)
        throws IOException{
        JavaType javaType = OBJECT_MAPPER.get().getTypeFactory().constructCollectionType(collectionClass,elementClass);
        return OBJECT_MAPPER.get().readValue(json,javaType);
    }


    public static ArrayList toList(String json) throws IOException {
        JsonUtil.json = json;
        ArrayList list = new ArrayList();
        JsonParser jsonParser = JSON_FACTORY.get().createParser(json.getBytes());
        JsonToken jsonToken = jsonParser.nextToken();
        if (jsonToken != JsonToken.START_ARRAY){
            System.out.println("json数据并不映射ArrayList");
            return list;
        }
        while (jsonToken != JsonToken.END_ARRAY){
            jsonToken = jsonParser.nextToken();
            if (jsonToken == JsonToken.VALUE_STRING){
                String str = jsonParser.getValueAsString();
                list.add(str);
            }
        }
        jsonParser.close();
        return list;
    }

    public static <K,V> Map<K,V> toMap(String json,Class<? extends Map> mapClaa,
                                       Class<K> keyClass, Class<V> valueClass) throws IOException{
        JavaType javaType = OBJECT_MAPPER.get().getTypeFactory().constructMapType(mapClaa,keyClass,valueClass);
        return OBJECT_MAPPER.get().readValue(json,javaType);
    }

    /**
     * 禁止调用无参构造
     * @throws AssertionError
     */
    private JsonUtil() throws  AssertionError{
        throw new AssertionError("can't create an instance!");
    }

    /**
     * 使用ThreadLocal创建对象，防止出现线程安全问题
     */
    private static final ThreadLocal<ObjectMapper> OBJECT_MAPPER = new ThreadLocal<ObjectMapper>(){
        @Override
        protected ObjectMapper initialValue(){
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);//忽略不存在的字段
            return objectMapper;
        }
    };

    private static final ThreadLocal<JsonFactory> JSON_FACTORY  = new ThreadLocal<JsonFactory>(){

        @Override
        protected JsonFactory initialValue(){
            JsonFactory jsonFactory = new JsonFactory();
            return jsonFactory;
        }
    };
}
